<template>
  <el-dialog
    :title="showTitle"
    width="40%"
    :visible="showDialog"
    @close="handleClose"
  >
    <el-form ref="perList" label-width="100px" :model="perList" :rules="perRules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="perList.name" style="width: 90%" size="small" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="perList.code" style="width: 90%" size="small" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="perList.description" style="width: 90%" size="small" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="perList.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item>
        <el-row style="width: 80% " type="flex" justify="center">
          <el-button size="small" type="primary" @click="confirmClick">确认</el-button>
          <el-button size="small" @click="handleClose">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPermissionAPI, getPermissionsDetailAPI, getPermissionsListAPI, updatePermissionAPI } from '@/api/permission'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    },
    type: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      perList: {
        name: '', // 权限点名字
        code: '', // 权限点标识
        description: '', // 权限点描述
        type: 1, // 权限点类型
        pid: 0, // 权限点父级id
        enVisible: '1', // 权限点开启状态
        id: null // 权限点id
      },
      perRules: {
        name: [
          { required: true, message: '权限名不能为空', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 获取权限点列表
              let perList = await getPermissionsListAPI()
              if (this.type === 'edit') {
                perList = perList.filter(item => item.id !== this.currentId)
              }
              // 利用数组的some方法，判断用户输入的名称是否已经存在
              if (perList.some(item => item.name === value)) {
                callback(new Error('权限名已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 获取权限点列表
              let perList = await getPermissionsListAPI()
              if (this.type === 'edit') {
                perList = perList.filter(item => item.id !== this.currentId)
              }
              // 利用数组的some方法，判断用户输入的标识是否已经存在
              if (perList.some(item => item.code === value)) {
                callback(new Error('权限标识已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.type === 'add' ? '新建权限点' : '编辑权限点'
    }
  },
  methods: {
    // 获取当前详情并回显到页面上
    async getPermissionsDetail() {
      if (this.type === 'edit') {
        this.perList = await getPermissionsDetailAPI(this.currentId)
      }
    },
    // 关闭弹层
    handleClose() {
      // 清空表单
      this.$refs.perList.resetFields()
      // 关闭弹层
      this.$emit('update:showDialog', false)
    },
    confirmClick() {
      if (this.type === 'add') this.perList.pid = this.currentId
      this.perList.pid > 0 ? this.perList.type = 0 : this.perList.type = 1
      this.$nextTick(() => {
        // 对表单进行整体校验
        this.$refs.perList.validate(async(isOk) => {
          if (isOk) {
            if (this.type === 'add') {
              // 调用添加权限接口
              await addPermissionAPI({ ...this.perList, id: null })
              // 提示用户添加成功
              this.$message.success('添加权限成功')
            } else {
              // 调用修改权限接口
              await updatePermissionAPI(this.perList)
              // 提示用户修改成功
              this.$message.success('修改权限成功')
            }
            // 通知父组件更新数据
            this.$emit('updatePer')
            // 关闭弹层+清空表单
            this.handleClose()
          }
        })// 对表单进行整体校验
      })
    }
  }
}
</script>

<style>

</style>
